﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using BusinessObjects;
using System.Data.SqlClient;

namespace DataAccess.SqlServer
{
    public class SqlServerBoPhan : IBoPhan
    {
        public BoPhan GetBoPhan(int oid)
        {
            string sql = @" SELECT MaQuanLy, TenBoPhan, LoaiBoPhan, TrucThuoc
                            FROM BoPhan
                            WHERE Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", oid);

            //string tam;
            //if (string.IsNullOrEmpty(tam))
            //    param = new SqlParameter("@Tam", DBNull.Value);
            //else
            //    param = new SqlParameter("@Tam", tam);

            DataRow row = DataProvider.GetDataRow(sql, param);

            string maQuanLy = row["MaQuanLy"].ToString();
            string tenBoPhan = row["TenBoPhan"].ToString();
            int trucThuoc;
            ELoaiBoPhan loaiBoPhan = (ELoaiBoPhan)row["LoaiBoPhan"];

            BoPhan boPhan = new BoPhan()
            {
                Oid = oid,
                MaQuanLy = maQuanLy,
                TenBoPhan = tenBoPhan,
                LoaiBoPhan = loaiBoPhan
            };

            if (int.TryParse(row["TrucThuoc"].ToString(), out trucThuoc))
                if (trucThuoc > 0)
                    boPhan.TrucThuoc = GetBoPhan(trucThuoc);

            return boPhan;
        }

        public List<BoPhan> GetListBoPhan()
        {
            string sql = @" SELECT Oid, MaQuanLy, TenBoPhan, LoaiBoPhan, TrucThuoc FROM BoPhan";

            DataTable dt = DataProvider.GetDataTable(sql);
            List<BoPhan> list = new List<BoPhan>();

            foreach (DataRow row in dt.Rows)
            {
                BoPhan boPhan = new BoPhan();

                boPhan.Oid = int.Parse(row["Oid"].ToString());
                boPhan.MaQuanLy = row["MaQuanLy"].ToString();
                boPhan.TenBoPhan = row["TenBoPhan"].ToString();
                byte loaiBoPhan;
                if (byte.TryParse(row["LoaiBoPhan"].ToString(), out loaiBoPhan))
                    boPhan.LoaiBoPhan = (ELoaiBoPhan)loaiBoPhan;
                int trucThuoc;
                if (int.TryParse(row["TrucThuoc"].ToString(), out trucThuoc))
                    if (trucThuoc > 0)
                        boPhan.TrucThuoc = GetBoPhan(trucThuoc);
                list.Add(boPhan);
            }
            return list;
        }

        public int Insert(BoPhan boPhan)
        {
            string sql = @" Insert into BoPhan(MaQuanLy, TenBoPhan, LoaiBoPhan, TrucThuoc)          
                            Values(@MaQuanLy, @TenBoPhan, @LoaiBoPhan, @TrucThuoc)";
            SqlParameter[] param = new SqlParameter[4];

            param[0] = new SqlParameter("@MaQuanLy", boPhan.MaQuanLy);
            param[1] = new SqlParameter("@TenBoPhan", boPhan.TenBoPhan);
            param[2] = new SqlParameter("@LoaiBoPhan", boPhan.LoaiBoPhan);
            if (boPhan.TrucThuoc != null)
                param[3] = new SqlParameter("@TrucThuoc", boPhan.TrucThuoc.Oid);
            else
                param[3] = new SqlParameter("@TrucThuoc", DBNull.Value);

            boPhan.Oid = DataProvider.Insert(sql, true, param);
            return boPhan.Oid;
        }

        public int Update(BoPhan boPhan)
        {
            string sql = @" Update BoPhan
                            Set MaQuanLy = @MaQuanLy,
                                TenBoPhan = @TenBoPhan,
                                LoaiBoPhan = @LoaiBoPhan,
                                TrucThuoc = @TrucThuoc
                            Where Oid = @Oid";
            SqlParameter[] param = new SqlParameter[5];

            param[0] = new SqlParameter("@MaQuanLy", boPhan.MaQuanLy);
            param[1] = new SqlParameter("@TenBoPhan", boPhan.TenBoPhan);
            param[2] = new SqlParameter("@LoaiBoPhan", boPhan.LoaiBoPhan);
            if (boPhan.TrucThuoc != null)
                param[3] = new SqlParameter("@TrucThuoc", boPhan.TrucThuoc.Oid);
            else
                param[3] = new SqlParameter("@TrucThuoc", DBNull.Value);
            param[4] = new SqlParameter("@Oid", boPhan.Oid);
            return DataProvider.Update(sql, param);
        }

        public int Delete(BoPhan boPhan)
        {
            string sql = @" Delete From BoPhan Where Oid = @Oid";

            SqlParameter param = new SqlParameter("@Oid", boPhan.Oid);
            return DataProvider.Update(sql, param);
        }


        public int Save(BoPhan obj)
        {
            throw new NotImplementedException();
        }
    }
}
